package edu.pw.treegrid.server.view;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import edu.pw.treegrid.server.filter.Filter;
import edu.pw.treegrid.server.message.Hierarchy;
import edu.pw.treegrid.server.message.Path;
import edu.pw.treegrid.server.message.Request;
import edu.pw.treegrid.server.message.ResponseFetchChilds;
import edu.pw.treegrid.server.model.DomainField;
import edu.pw.treegrid.server.model.DomainObject;
import edu.pw.treegrid.server.reportmodel.Report;
import edu.pw.treegrid.server.reportmodel.ReportSimpleColumn;
import edu.pw.treegrid.server.service.TreeGridService;
import edu.pw.treegrid.shared.ReportColumnType;

public class ExcelView {
	
	public ExcelView(TreeGridService service) {
		super();
		this.service = service;
	}
	
	private TreeGridService service;

	public void setService(TreeGridService service) {
		this.service = service;
	}
	
	public Workbook buildExcelDocument(Report report, Filter filter) {
		Workbook wb = new HSSFWorkbook();
		Sheet sheet;
        Cell cell;

        sheet = wb.createSheet(report.getId());
        sheet.setDefaultColumnWidth((short) 12);
        
        Request request = new Request();
        request.setFilter(filter);
        request.setHierarchy(new Hierarchy());
        Path path = new Path();
        path.addNode("null");
        request.setPath(path);
        request.setReport(report);
        ResponseFetchChilds result = new ResponseFetchChilds();
        service.fetchChilds(request, result);
        
        int startRow = 0;
        int rowNumber = startRow;
        for(DomainObject object : result.getDomainObjects()) {
        	int cellNumber = 0;
        	
        	//Wyswietl naglowek:
        	if(rowNumber == startRow) {
        		for(DomainField df : object.getDomainField()) {
        			cell = getCell(sheet, rowNumber, cellNumber);
        			cell.setCellType(Cell.CELL_TYPE_STRING);
            		ReportSimpleColumn rsc = df.getReportColumn().getReportSimpleColumn();
            		cell.setCellValue(service.getColumnTitle(rsc));
            		cellNumber++;
        		}
        		cellNumber = 0;
        		rowNumber++;
        	}
        	//Wyswietl dane
        	for(DomainField df : object.getDomainField()) {
        		cell = getCell(sheet, rowNumber, cellNumber);
        		ReportSimpleColumn rsc = df.getReportColumn().getReportSimpleColumn();
        		int cellType = Cell.CELL_TYPE_STRING;
        		Object cellValue = df.getValue();
        		if(rsc.getType() == ReportColumnType.INTEGER || rsc.getType() == ReportColumnType.FLOAT) {
        			cellType = Cell.CELL_TYPE_NUMERIC;
        		}
        		
        		if(cellValue == null || cellValue.equals("")) {
        			cell.setCellType(Cell.CELL_TYPE_BLANK);
        		}
        		else {
        			cell.setCellType(cellType);
        			if(cellType == Cell.CELL_TYPE_NUMERIC) {
            			cell.setCellValue(Double.parseDouble(cellValue.toString()));
            		}
            		else {        			
            			cell.setCellValue(cellValue.toString());
            		}
        		}
        		
        		cellNumber++;
        	}
        	rowNumber++;
        }
        
        return wb;
	}

	private Cell getCell(Sheet sheet, int row, int col) {
		Row sheetRow = sheet.getRow(row);
		if (sheetRow == null) {
			sheetRow = sheet.createRow(row);
		}
		Cell cell = sheetRow.getCell((short) col);
		if (cell == null) {
			cell = sheetRow.createCell((short) col);
		}
		return cell;
	}	

}
